﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zcu.PanelComposer.ViewModels.Core;
using Zcu.PanelComposer.Interfaces;
using System.ComponentModel.Composition;
using Caliburn.Micro;
using Zcu.PanelComposer.Models.Model;
using Zcu.PanelComposer.ViewModels.Events;

namespace Zcu.PanelComposer.ViewModels
{
    /// <summary>
    /// ViewModel pro existujici projekty.
    /// </summary>
    [Export(typeof(IModule))]
    public class ExistingProjectsViewModel : NavigateScreen
    {
        #region Fields

        /// <summary>
        /// Reference na logger.
        /// </summary>
        private readonly ILog _log = LogManager.GetLog(typeof(ExistingProjectsViewModel));

        /// <summary>
        /// Kolekce vsech projektu.
        /// </summary>
        private IObservableCollection<Project> _Project;

        /// <summary>
        /// Prave aktivni projekt.
        /// </summary>
        private Project _SelectedProject;

        #endregion // Fields

        #region Properties

        /// <summary>
        /// Pristup na databazi.
        /// </summary>
        [Import]
        public IRepository Repository { get; private set; }

        /// <summary>
        /// Kolekce vsech projektu.
        /// </summary>
        public IObservableCollection<Project> Project
        {
            get { return _Project; }
            set
            {
                if (_Project != value) _Project = value;
                NotifyOfPropertyChange(() => Project);
            }
        }

        /// <summary>
        /// Prave aktivni projekt.
        /// </summary>
        public Project SelectedProject
        {
            get { return _SelectedProject; }
            set
            {
                if (_SelectedProject != value) _SelectedProject = value;

                NotifyOfPropertyChange(() => SelectedProject);
                NotifyOfPropertyChange(() => CanActiveAction);
                NotifyOfPropertyChange(() => CanRemoveAction);
            }
        }

        #endregion

        #region Guard properties

        /// <summary>
        /// Hlidani, zdali lze aktivovat projekt.
        /// </summary>
        public bool CanActiveAction { get { return SelectedProject != null; } }

        /// <summary>
        /// Hlidani, zdali lze smazat projekt.
        /// </summary>
        public bool CanRemoveAction { get { return SelectedProject != null; } }

        #endregion

        #region Constructor

        /// <summary>
        /// Konstruktor ViewModelu, prednastaveni pocatecnich hodnot.
        /// Nazev stranky (titulek), nazev modulu (kvuli navigaci) a poradni v menu.
        /// </summary>
        public ExistingProjectsViewModel()
        {
            /// todo do resources
            DisplayName = "Existing Projects";
            ModuleName = PageNames.ExistingProjects;
            Order = 2;
        }

        #endregion // Constructor

        #region Overrided methods

        /// <summary>
        /// Pri aktivaci okna zkontrolujeme zda jde o editaci ci zalozeni noveho.
        /// Nasledne nacteme data z projektu do ViewModelu.
        /// </summary>
        protected override void OnActivate()
        {
            base.OnActivate();

            // nacteni dat z instance projektu
            LoadData();
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Akce, vyvolana stiskem tlacitka "New project".
        /// </summary>
        public void ActiveAction()
        {
            // zmena aktivniho projektu
            EventAggregator.Publish(new ActiveProjectEvent(SelectedProject));
            Navigate(PageNames.ProjectDetail);
        }

        /// <summary>
        /// Odstraneni projektu.
        /// </summary>
        public void RemoveAction()
        {
            if (Repository.GetAll<SolutionProject>(s => s.Project == SelectedProject).Count > 0)
            {
                System.Windows.MessageBox.Show("This project is already used in Solution(s), please, remove link before delete project."
                    , "Information dialog"
                    , System.Windows.MessageBoxButton.OK
                    , System.Windows.MessageBoxImage.Information);
            }
            else
            {
                var drawing = Repository.GetById<Drawing>(d => SelectedProject.Drawing == d);
                if (drawing != null)
                    Repository.Delete<Drawing>(drawing);

                Repository.Delete<Project>(SelectedProject);
                Repository.Commit();

                LoadData();
            }


        }

        #endregion // Public methods

        #region Private methods

        /// <summary>
        /// Nacteni dat o projektech z DB.
        /// </summary>
        private void LoadData()
        {
            // nacteni vsech projektu z DB
            Project = new BindableCollection<Project>(Repository.GetAll<Project>());
        }

        #endregion
    }
}
